<?php


namespace App\Api\Controllers;


use App\Models\Category;
use App\Models\UserFeedback;
use App\Models\UserMessage;
use App\Models\UserMoneyLog;
use App\Models\UserScoreLog;
use App\User;
use Illuminate\Http\Request;
use JWTAuth;

class UserController extends Controller
{
    public function addUser(Request $request)
    {
        try{
            $input = $request->only([
                'realname',
                'mobile',
                'store_id',
            ]);
            $input['region_id'] = Category::getParentId($input['store_id']);
            $user = new User();
            foreach($input as $key=>$val){
                if(!empty($input[$key])){
                    $user->$key = $input[$key];
                }
            }
            $user->save();
            return $this->responseData($user->toArray());
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    public function userList(Request $request)
    {
        try{
            $user = auth()->user();
            $data = User::getStoreUserId($user['store_id']);
            return $this->responseData($data);
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    public function deluser(Request $request)
    {
        try{
            $user = User::find($request->user_id);
            if(!$user){
                return $this->responseFailed('用户不存在');
            }
            if($user->delete()){
                return $this->responseSuccess();
            } else {
                return $this->responseFailed('用户操作失败');
            }
        }catch (\Exception $e){
            return $this->responseFailed($e->getMessage());
        }
    }

    //资金明细
    public function moneyLog(Request $request)
    {
        $user = auth()->user();
        /*
        if (!$user) {
            return $this->responseFailed('登录超时');
        }*/
        $where = [
            ['user_id', '=', $user['id']],
        ];
        if ($request->type == 1) {
            $where[] = ['type', '=', 1];
        }
        if ($request->type == 2) {
            $where[] = ['type', '=', 2];
        }
        $log = UserMoneyLog::where($where)->paginate(10)->toArray();
        $log['money'] = $user['money'];
        return $this->responseData($log);
    }

    //积分明细
    public function scoreLog()
    {
        $user = auth()->user();
        $where = [
            ['user_id', '=', $user['id']],
        ];
        $log = UserScoreLog::where($where)->paginate(10)->toArray();
        $log['score'] = $user['score'];
        return $this->responseData($log);
    }


    //用户中心
    public function info()
    {
        $user = auth()->user();
        $data = [];
        if ($user) {
            $data['user'] = $user->toArray();
        }
        return $this->responseData($data);
    }

    //系统消息
    public function messages()
    {
        $data = UserMessage::getPageList();
        return $this->responseData($data);
    }

    //意见反馈
    public function feedback(Request $request)
    {
        if (!$request->bref) {
            return $this->responseFailed('问题描述必填');
        }
        if (!$request->contact) {
            return $this->responseFailed('联系方式必填');
        }
        $user = auth()->user();
        $feedback = new UserFeedback();
        $feedback->bref = $request->bref;
        $feedback->user_id = $user['id'];
        $feedback->contact = $request->contact;
        if ($feedback->save()) {
            return $this->responseSuccess();
        }
        return $this->responseFailed();
    }

    public function logout()
    {
        JWTAuth::parseToken()->invalidate();
        return $this->responseSuccess();
    }
}